#!/usr/bin/env python
#coding=utf-8

import os,web,time
import sqlite3 as db

urls = (
        '/', 'hello',
        '/add','add'
        )
render = web.template.render('templates/')

class hello:
    def GET(self):
        #实例化sqldb，然后获取内容
        s = ""
        sdb = sqldb()
        rec = sdb.cu.execute("""select * from msgs""")
        dbre = sdb.cu.fetchall()
        web.header("Content-Type","text/html; charset=utf-8")
        return render.index(dbre)


class add:
    def POST(self):
        i = web.input('content')
        n = web.input('uname')
        date = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
        sdb = sqldb()
        rec = sdb.cu.execute("""select * from msgs""")
        dbre = sdb.cu.fetchall()
        for k in dbre:
            j = k[0]+1
        t = (j,n.uname,date,i.content)
        sdb.cu.execute('insert into msgs values(?,?,?,?)',t)
        sdb.conn.commit()
        return web.seeother('/')

    def GET(self):
        return web.seeother('/')

class sqldb:
    #先验证数据库是否存在
    def __init__(self):
        if os.path.exists("msg.db"):
            #如果数据库存在，就直接连接
            self.conn = db.connect("msg.db")
            self.cu = self.conn.cursor()
        else:
            #如果数据库不存在，连接，并生成表
            self.conn = db.connect("msg.db")
            self.cu = self.conn.cursor()
            self.cu.execute("""create table msgs(
                     id integer primary key,
                     name text,
                     date text,
                     content text) """)
            self.cu.execute("""insert into msgs values(1,'Ahai','2010-05-19 15:11:20','Ahi alaws be ok!')""")
            self.conn.commit()

if __name__=="__main__":

    app = web.application(urls,globals())
    app.run()
